﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EWDTAssignment.App_Code;
using System.Data;

namespace EWDTAssignment
{
    public partial class Confirmation1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //REMEMBER TO DELETE!
            Session["AccountID"] = 1;
            if (Session["AccountID"] != null)
            {
                if (!IsPostBack)
                {
                    Accounts acc = EntityLogic.RetrieveAccountByID(Convert.ToInt32(Session["AccountID"]));

                    if (acc.Address != null && acc.Address != "")
                    {
                        txtbxAddress.Text = acc.Address;
                        txtbxPostal.Text = acc.PostalCode.ToString();
                        txtbxAddress.Enabled = false;
                        txtbxPostal.Enabled = false;
                        btnEdit.Visible = true;
                        btnSave.Visible = false;

                        Dictionary<int, int> checkoutList = (Dictionary<int, int>)Session["checkoutList"];
                        DataTable dt = new DataTable();
                        dt.Columns.Add("Count", typeof(int));
                        dt.Columns.Add("ProductName", typeof(string));
                        dt.Columns.Add("Quantity", typeof(int));
                        dt.Columns.Add("Price", typeof(double));

                        int count = 1;
                        foreach (KeyValuePair<int, int> kvp in checkoutList)
                        {
                            DataRow dr = dt.NewRow();
                            dr["Count"] = count;
                            count++;
                            Products pro = EntityLogic.RetrieveProductByID(kvp.Key);
                            dr["ProductName"] = pro.ProductName;
                            dr["Quantity"] = kvp.Value;
                            dr["Price"] = pro.Price;
                            dt.Rows.Add(dr);

                       }
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                        lblGtotal.Text = Session["checkoutGtotal"].ToString();
                    }
                    else
                    {
                        Response.Redirect("EditProfile.aspx");
                    }
                }
            }
            else
            {
                Response.Redirect("Login.aspx");
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            txtbxAddress.Enabled = true;
            txtbxPostal.Enabled = true;
            btnSave.Visible = true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            EntityLogic.UpdateAddress(Convert.ToInt32(Session["AccountID"]), txtbxAddress.Text, Convert.ToInt32(txtbxPostal.Text));
            btnSave.Visible = false;
            txtbxAddress.Enabled = false;
            txtbxPostal.Enabled = false;
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            
        }


    }
}